package servlets;

import java.io.IOException;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ibm.eg.cvmanager.business.interfaces.InterviewerBusiness;
import com.ibm.eg.cvmanager.business.stub.InterviewerBusinessImpl;
import com.ibm.eg.cvmanager.dto.Interview;
import com.ibm.eg.cvmanager.dto.Interviewer;
import com.ibm.eg.cvmanager.dto.InterviewerId;

public class UpdateInterviewerServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void service(HttpServletRequest  req, HttpServletResponse res)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("interviewId");
		String oldName = req.getParameter("oldName");
		String submitBtn = req.getParameter("submitBtn");
		String newName = req.getParameter("interviewerName");
		Interviewer interviewer = new Interviewer();
		InterviewerId interviewerId = new InterviewerId();
		interviewer.setId(interviewerId);
		InterviewerBusiness interviewerBusiness = new InterviewerBusinessImpl();
		if (submitBtn == "Add") {
			
		}
		else {
			interviewerId.setInterviewId(Integer.parseInt(id));
			interviewerId.setName(oldName);
			interviewerBusiness.updateInterviewer(interviewer, newName);
			HttpSession session = req.getSession();
			
			Interview interview = (Interview) session.getAttribute("interview");
			Set interviews = interview.getInterviewers();
			
			for (Object interviewerObj : interviews) {
				Interviewer interviewerItem = (Interviewer)interviewerObj;
				if(interviewerItem.getId().getName() == oldName) {
					interviewerItem.getId().setName(newName);
					break;
				}
			}
			session.setAttribute("interview", interview);
		}
		
		String nextJSP = "/addpage.jsp";
		
		RequestDispatcher dispatcher = req.getRequestDispatcher(nextJSP);
		dispatcher.forward(req,res);
		

	}

}
